Conditions | 10 |
Total Lines | 108 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like yii2-floor12-files.js ➔ Yii2FilesUploaderSet often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | console.log('Yii2 files model init.'); |
||
61 | function Yii2FilesUploaderSet(id, className, attribute, scenario) { |
||
62 | |||
63 | var mode = 'multi'; |
||
64 | var blockName = "#" + id; |
||
65 | var block = $(blockName); |
||
66 | var uploadButton = block.find('button.btn-upload')[0]; |
||
67 | var filesList = block.find('.floor12-files-widget-list')[0]; |
||
68 | var ratio = 0; |
||
69 | |||
70 | var csrf = block.parents('form').find('input[name=' + yii2CsrfParam + ']').val(); |
||
71 | |||
72 | if (block.data('ratio')) |
||
73 | ratio = block.data('ratio'); |
||
74 | |||
75 | if (block.hasClass('floor12-files-widget-single-block')) { |
||
76 | mode = 'single'; |
||
77 | toggleSingleUploadButton(block); |
||
78 | } |
||
79 | |||
80 | var data = { |
||
81 | modelClass: className, |
||
82 | attribute: attribute, |
||
83 | scenario: scenario, |
||
84 | mode: mode, |
||
85 | ratio: ratio, |
||
86 | _fileFormToken: yii2FileFormToken |
||
87 | } |
||
88 | data[yii2CsrfParam] = csrf |
||
89 | |||
90 | var uploader = new ss.SimpleUpload({ |
||
91 | button: uploadButton, |
||
92 | url: yii2UploadRoute, |
||
93 | name: 'file', |
||
94 | dropzone: block, |
||
95 | dragClass: 'floor12-files-widget-block-drug-over', |
||
96 | multiple: true, |
||
97 | multipleSelect: true, |
||
98 | data: data, |
||
99 | onSubmit: |
||
100 | function (filename, extension, data) { |
||
101 | var svg = '\t<svg width="60" height="60" viewBox="0 0 60 60">\n' + |
||
102 | '\t\t<circle cx="30" cy="30" r="27" fill="none" stroke="#ccc" stroke-width="5" />\n' + |
||
103 | '\t\t<circle id="progress-circle" cx="30" cy="30" r="27" fill="none" stroke="#666" stroke-width="5" stroke-dasharray="169.646" stroke-dashoffset="169.646" />\n' + |
||
104 | '\t</svg>'; |
||
105 | |||
106 | var id = generateId(filename); |
||
107 | var btnGroup = document.createElement('div'); |
||
108 | var fileObject = document.createElement('div'); |
||
109 | var bar = document.createElement('div'); |
||
110 | var percents = document.createElement('div'); |
||
111 | btnGroup.setAttribute('id', id); |
||
112 | btnGroup.className = 'btn-group files-btn-group'; |
||
113 | fileObject.className = 'floor12-file-object'; |
||
114 | percents.className = 'floor12-file-percents'; |
||
115 | |||
116 | this.setProgressBar(bar); |
||
117 | |||
118 | fileObject.innerHTML = svg; |
||
119 | |||
120 | observer.observe(bar, { |
||
121 | attributes: true |
||
122 | }); |
||
123 | |||
124 | fileObject.appendChild(bar); |
||
125 | fileObject.appendChild(percents); |
||
126 | btnGroup.appendChild(fileObject); |
||
127 | |||
128 | |||
129 | if (mode == 'single') { |
||
130 | $(filesList).html(''); |
||
131 | } |
||
132 | $(filesList).append(btnGroup); |
||
133 | |||
134 | |||
135 | }, |
||
136 | onComplete: function (filename, response) { |
||
137 | if (!response) { |
||
138 | console.log(filename + 'upload failed'); |
||
139 | return false; |
||
140 | } |
||
141 | f12notification.info(FileUploadedText, 1); |
||
142 | idName = "#" + generateId(filename); |
||
143 | $(idName).replaceWith($(response)); |
||
144 | |||
145 | if (mode == 'single') |
||
146 | toggleSingleUploadButton(block); |
||
147 | }, |
||
148 | onError: function (filename, errorType, status, statusText, response, uploadBtn, fileSize) { |
||
149 | data = { |
||
150 | responseText: response, |
||
151 | status: status, |
||
152 | statusText: statusText, |
||
153 | }; |
||
154 | processError(data); |
||
155 | idName = "#" + generateId(filename); |
||
156 | $(idName).remove(); |
||
157 | } |
||
158 | |||
159 | // progressUrl: 'uploadProgress.php', // enables cross-browser progress support (more info below) |
||
160 | // responseType: 'json', |
||
161 | // allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], |
||
162 | // maxSize: 1024, // kilobytes |
||
163 | // hoverClass: 'ui-state-hover', |
||
164 | // focusClass: 'ui-state-focus', |
||
165 | // disabledClass: 'ui-state-disabled', |
||
166 | }); |
||
167 | |||
168 | } |
||
169 | |||
365 |